package org.com.sec.noncritical.enigma;

import java.util.*;

/**
 * Provides Swiss-K Enigma machine encryption.
 *
 * @author See <a href="the Google Code page of the easy-utils
 * library">http://code.google.com/p/easy-utils/</a> for collaborators and other
 * information.
 */
public abstract class SwissK {

    /**
     * Indicates the starting positions for the Enigma Machine rotors. Take into
     * account it resets within application closure.
     */
    private static String defaultRotorsStartingPositions = "A,A,A";

    /**
     * Getter for {@link Security#defaultRotorsStartingPositions}.
     *
     * @return {@link String} {@link Security#defaultRotorsStartingPositions}.
     */
    public static String getDefaultRotorsStartingPositions() {
        return SwissK.defaultRotorsStartingPositions;
    }

    /**
     * Setter for {@link Security#defaultRotorsStartingPositions}.
     */
    public static void setDefaultRotorsStartingPositions(String newDefaultRotorsStartingPositions) {
        StringTokenizer tokenizer = new StringTokenizer(newDefaultRotorsStartingPositions, ",");

        try {
            tokenizer.nextToken();
            tokenizer.nextToken();
            tokenizer.nextToken();
            try {
                tokenizer.nextToken();
            } catch (NoSuchElementException ex) {
                SwissK.defaultRotorsStartingPositions = newDefaultRotorsStartingPositions;
                return;
            }
        } catch (NoSuchElementException ex) {
        }
        throw new IllegalArgumentException("Illegal value for Security.defaultRotorsStartingPositions - " + newDefaultRotorsStartingPositions);
    }

    /**
     * Ciphers a message by simulating a three rotors Swiss-K Enigma machine.
     * <b>Please note this method is marked as deprecated since this is a broken
     * algorithm, and therefore it shouldn't be used in any safety-critical
     * application. Use at your own risk.</b>
     *
     * @param message {@link String} The message to cipher. Non-ASCII simplified
     * characters won't receive any modifications.
     * @param startingPositions {@link String} The starting characters for the
     * rotors. They must be separated by a comma each. If <value>null</value>,
     * it's taken as the {@link Security#defaultRotorsStartingPositions} value.
     * @param steckers {@link String} The steckers reallocations. Each
     * reallocation consists of two characters meaning that they're both plugged
     * to each other. Each reallocation must be separated from the next by a
     * comma.
     * @return {@link String} The ciphered message.
     */
    @Deprecated
    public static String applyEnigmaEncryption(String message, String startingPositions, String steckers) {
        ArrayList<String> params = new ArrayList<>();

        params.add(message);
        if (startingPositions != null) {
            params.add(startingPositions);
        } else {
            params.add(SwissK.defaultRotorsStartingPositions);
        }
        if (steckers != null) {
            params.add(steckers);
        }

        return EnigmaMachine.execute(params);
    }
}
